﻿
using KouDai.WinPad.Helper;
using NextEngine.PortInterface;
using System.IO;
using System.Threading.Tasks;


namespace KouDai.WinPad.ImplInterface
{
    public class WinRTCompress : ICompress
    {
        public  async Task<byte[]> CompressStream(System.Net.WebResponse response)
        {
            using (Stream stream = response.GetResponseStream())
            {
                Stream responseStream = stream;
                if (response.Headers["gzipType"] != null && response.Headers["gzipType"].Equals("1"))
                {
                    responseStream = stream.UnZipStream();
                }
                using (MemoryStream ms = new MemoryStream())
                {
                    byte[] buffer = new byte[8 * 1024];
                    int read = 0;
                    while ((read = await responseStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
                    {
                        ms.Write(buffer, 0, read);
                    }
                    return ms.ToArray();
                }
            }
        }
    }
}
